package com.banshuibao.bsb_android.activity;

import android.Manifest;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banshuibao.bsb_android.MyApplication;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.util.DataSet;
import com.banshuibao.bsb_android.util.GetLoginInfo;
import com.banshuibao.bsb_android.util.VersionCodeUtil;
import com.banshuibao.bsb_android.util.XToast;
import com.banshuibao.bsb_android.view.HomeBottomMenu;

/**
 * Created by zyh on 2017/12/18.
 */

public class BaseHomeActivity extends PermissionBaseActivity {

    private long mExitTime;

    TextView home, video, answer, mine;

    ImageView home_iv, video_iv, answer_iv, mine_iv;

    RelativeLayout home_layout, video_layout, answer_layout, mine_layout;

    FrameLayout moreLayout;

    ImageView moreImage;

    HomeBottomMenu homeBottomMenu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initUpdate();
        if (!TextUtils.isEmpty(applicationContext.getLoginInfo().getUserId())) {
            DataSet.init(MyApplication.applicationInstance.getApplicationContext(), applicationContext.getLoginInfo().getUserId());
        } else {
            DataSet.clear();
        }
    }

    void initView() {
        homeBottomMenu = new HomeBottomMenu(this);
        home = findViewById(R.id.home);
        video = findViewById(R.id.video);
        answer = findViewById(R.id.answer);
        mine = findViewById(R.id.mine);
        home_iv = findViewById(R.id.home_iv);
        video_iv = findViewById(R.id.video_iv);
        answer_iv = findViewById(R.id.answer_iv);
        mine_iv = findViewById(R.id.mine_iv);
        home_layout = findViewById(R.id.home_layout);
        video_layout = findViewById(R.id.video_layout);
        answer_layout = findViewById(R.id.answer_layout);
        mine_layout = findViewById(R.id.mine_layout);
        moreLayout = findViewById(R.id.more);
        moreImage = findViewById(R.id.more_img);

        home_layout.setOnClickListener(listener);
        video_layout.setOnClickListener(listener);
        answer_layout.setOnClickListener(listener);
        mine_layout.setOnClickListener(listener);
        moreLayout.setOnClickListener(listener);
    }

    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.home_layout:
                    showFragment(R.id.home);
                    break;
                case R.id.video_layout:
                    showFragment(R.id.video);
                    break;
                case R.id.answer_layout:
                    showFragment(R.id.answer);
                    break;
                case R.id.mine_layout:
                    showFragment(R.id.mine);
                    break;
                case R.id.more:
                    showBottomMenu();
                    break;
            }
        }
    };

    void showFragment(int id) {

    }

    private RotateAnimation rotateAnimation;

    void showBottomMenu() {
        if (rotateAnimation == null) {
            rotateAnimation = new RotateAnimation(
                    0f, -45f,
                    Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f);
            rotateAnimation.setDuration(400);
            rotateAnimation.setFillAfter(false);
        }
        moreImage.startAnimation(rotateAnimation);
    }


    private void initUpdate() {
        if (!TextUtils.isEmpty(GetLoginInfo.getLoginInfoInstance(this).readUser().getCookieId())) {
            requestUserPermission(View.inflate(context, R.layout.activity_permission_layout, null), Manifest.permission.WRITE_EXTERNAL_STORAGE, 210);
        }
    }

    @Override
    public void successGrantPermission(int requestCode) {
        switch (requestCode) {
            case 210:
                new VersionCodeUtil(context).getVersionCode();
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                XToast.showToast(this, "再按一次退出程序");
                mExitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        DataSet.saveData();
        super.onDestroy();
    }
}
